package com.example.service.impl;

import com.example.mapper.WorkMapper;
import com.example.pojo.Collection;
import com.example.pojo.Comment;
import com.example.pojo.Work;
import com.example.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import java.util.List;

@Service
public class WorkServiceImpl  implements WorkService {
    @Autowired
    private WorkMapper workMapper;

    @Override
    public List<Work> findAll() {
        List<Work> works = workMapper.findAll();
        works.forEach(work -> {
            System.out.println("Work ID: " + work.getId());
            System.out.println("Description length: " + 
                (work.getDescription() != null ? work.getDescription().length() : 0));
            System.out.println("Image URL length: " + 
                (work.getImgUrl() != null ? work.getImgUrl().length() : 0));
        });
        return works;
    }

    @Override
    public void insert(Work work) {
        System.out.println("Inserting work with userId: " + work.getUserId());
        work.setPublishTime(LocalDateTime.now());
        workMapper.insert(work);
    }

    @Override
    public void insertComment(Comment comment) {
        System.out.println("Inserting work with userId: " + comment.getUserId());
        comment.setPublishTime(LocalDateTime.now());
        workMapper.insertComment(comment);
    }



    @Override
    public void incrementViews(Integer id) {
        workMapper.incrementViews(id);
    }

    @Override
    public void incrementLikes(Integer id) {
        workMapper.incrementLikes(id);
    }
    @Override
    public List<Comment> findCommentsByWorksId(Integer workId) {
        return workMapper.findCommentsByWorksId(workId);
    }

    @Override
    public Work findById(Integer id) {
        return workMapper.findById(id);
    }

    @Override
    public List<Work> findByUserId(Integer userId) {
        return workMapper.findByUserId(userId);
    }

    @Override
    public void deleteById(Integer id) {
        workMapper.deleteWork(id);
    }


    @Override
    public void insertCollection(Collection collection) {
        workMapper.insertCollection(collection);
    }

    @Override
    public List<Collection> getUserCollections(Integer userId) {
        return workMapper.selectCollectionsByUserId(userId);
    }

    @Override
    public boolean checkCollection(Integer userId, Integer workId) {
        return workMapper.checkCollection(userId, workId) ;
    }

    @Override
    public void deleteCollection(Integer userId, Integer workId) {
        workMapper.deleteCollection(userId, workId);
    }

    //检查是否收藏

}
